package com.jlnk.stockdemo.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jlnk.stockdemo.R;
import com.jlnk.stockdemo.utils.Constants;
import com.jlnk.stockdemo.utils.FileUtils;
import com.jlnk.stockdemo.utils.ImageUtils;
import com.jlnk.stockdemo.utils.SPUtils;
import com.jlnk.stockdemo.utils.ToastUtils;

import java.io.File;
import java.io.IOException;

public class RegisterActivity extends AppCompatActivity {
    private ImageView image_avator;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        image_avator = findViewById(R.id.image_avator);
        EditText et_account = findViewById(R.id.et_account);
        EditText et_password = findViewById(R.id.et_password);
        Button btn_register = findViewById(R.id.btn_register);
        image_avator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openImagePicker();
            }
        });
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = getEditText(et_account);
                String password = getEditText(et_password);
                if (!SPUtils.getUsername().equals("")) {
                    ToastUtils.showToast("账号已存在！");
                }
                else if (!account.equals("") || !password.equals("")) {
                    SPUtils.saveUsername(account);
                    SPUtils.savePassword(password);
                    ToastUtils.showToast("注册成功！");
                    startActivity(new Intent(RegisterActivity.this, MainActivity.class));
                    finish();
                } else {
                    ToastUtils.showToast("注册失败！账号或密码为空！");
                }
            }
        });
    }

    private void openImagePicker() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), Constants.REGISTER_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.REGISTER_REQUEST_CODE && resultCode == RESULT_OK) {
            if (data == null || data.getData() == null) {
                return;
            }
            Uri selectedImageUri = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
                image_avator.setImageBitmap(bitmap);
                // 将Bitmap转换为JPG文件
                String outputPath = getExternalFilesDir(null) + "/avatar.jpg"; // 使用外部存储的私有目录
                File jpgFile = ImageUtils.convertBitmapToJPG(bitmap, outputPath, 90); // 假设我们设置质量为90
                // 保存JPG文件到应用的私有目录
                String savedFilePath = FileUtils.saveJPGFile(this, jpgFile, "avatar.jpg");
                SPUtils.saveAvatorFilePath(savedFilePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private String getEditText(EditText editText) {
        if (editText != null &&
                editText.getText() != null &&
                editText.getText().toString() != null &&
                !editText.getText().toString().isEmpty()) {
            return editText.getText().toString();
        }
        return "";
    }
}
